import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import './plugins/element.js'
import './assets/css/global.css'
import './assets/iconfont/iconfont.css'
import './assets/js/string.js'
import {
  showLoading,
  hideLoading
} from './assets/js/loading.js'
// 导入nprogress
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import store from './store'
import ElementUI from 'element-ui'
// 多语言
import i18n from './i18n/i18n'

axios.defaults.baseURL = 'http://localhost:8831/api/' // 'http://10.33.22.154:8080//api/' // 'http://10.35.166.176:8831/api' //
// axios.defaults.timeout = 30000 // 超时时间

//  设置拦截器
axios.interceptors.request.use(config => {
  console.log(store.state.loginId)
  nprogress.start()
  showLoading()
  config.headers.Authorization = window.sessionStorage.getItem('token')
  return config
})

axios.interceptors.response.use(response => {
  nprogress.done()
  hideLoading()
  console.log('=============================')
  console.log(response)
  console.log('=============================')
  if (response.status === 203) {
    // Message.error(response.data.statusText)
    ElementUI.Message({
      showClose: true,
      message: '没有权限',
      type: 'error'
    })
    return Promise.reject(new Error('没有通过权限验证'))
  }
  return response
},
  error => {
    if (error.message.includes('timeout')) {
      return Promise.reject(error) // reject这个错误信息
    }
    hideLoading()
    return Promise.reject(error)
  })
Vue.prototype.$http = axios
Vue.config.productionTip = false

new Vue({
  router,
  store,
  i18n,
  render: h => h(App)
}).$mount('#app')
